package com.taotao.job.conf;

import com.taotao.job.core.trigger.Trigger;
import com.taotao.job.schedule.lock.DistributedLock;
import com.taotao.job.schedule.JobLogicScheduler;
import com.taotao.job.schedule.JobTimeScheduler;
import com.taotao.job.service.JobScheduleMetaService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author fanbeibei
 * @date 2021/3/7 18:22
 */
@Configuration
public class JobSchedulerCongfig implements InitializingBean, DisposableBean {
    private static JobSchedulerCongfig congfig;

    @Resource
    private DistributedLock distributedLock;

    @Resource
    private JobScheduleMetaService jobScheduleMetaService;


    private JobLogicScheduler jobLogicScheduler;
    private JobTimeScheduler jobTimeScheduler;

    private Trigger trigger;

    @Override
    public void afterPropertiesSet() throws Exception {
        congfig = this;

//        trigger = ;
        jobLogicScheduler = new JobLogicScheduler(trigger);
        jobTimeScheduler = new JobTimeScheduler(jobLogicScheduler, distributedLock, jobScheduleMetaService);

        jobLogicScheduler.start();
        jobTimeScheduler.start();

    }

    @Override
    public void destroy() throws Exception {
        jobTimeScheduler.stop();
        jobLogicScheduler.stop();
    }


    public static DistributedLock getDistributedLock() {
        return congfig.distributedLock;
    }

    public static JobLogicScheduler getJobLogicScheduler() {
        return congfig.jobLogicScheduler;
    }

    public static JobTimeScheduler getJobTimeScheduler() {
        return congfig.jobTimeScheduler;
    }

    public static JobScheduleMetaService getJobScheduleMetaService() {
        return congfig.jobScheduleMetaService;
    }
}
